package net.lzzy.practices.aspects;

import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.utils.AspectUtils;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.web.models.ApiResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 *
 * @author 86188
 */

@Component
@Aspect
public class CourseControllerAspect {
    @Pointcut("execution(* net.lzzy.practices.web.course.CourseApiController.getTeachersCourses(..)) ||"+
            "execution(* net.lzzy.practices.web.course.CourseApiController.add(..) ) ||"+
            "execution(* net.lzzy.practices.web.course.CourseApiController.update(..) ) ||"+
            "execution(* net.lzzy.practices.web.course.CourseApiController.openCloseCourse(..) ) ||"+
            "execution(* net.lzzy.practices.web.course.CourseApiController.delete(..))")
    private void teacherAspect(){}

    @Pointcut("execution(* net.lzzy.practices.web.course.CourseApiController.getStudentsCourses(..))")
    private void studentAspect(){}

    @Pointcut("execution(* net.lzzy.practices.web.course.CourseApiController.getCourses(..)) ||"+
            "execution(* net.lzzy.practices.web.course.CourseApiController.getOpenCourses(..) ) ||"+
            "execution(* net.lzzy.practices.web.course.CourseApiController.getPagedCourses(..) ) ||"+
            "execution(* net.lzzy.practices.web.course.CourseApiController.getPagedOpenCourses(..))")
    private void courseAspect(){}

    @Around("courseAspect()")
    public ApiResult aroundCourse(ProceedingJoinPoint point)throws Throwable{
        String token= AspectUtils.getToken(point);
        if(AuthUtils.illegalUser(token,new int[]{User.TYPE_STUDENT,User.TYPE_TEACHER})){
            return new ApiResult(false, Messages.NO_PERMISSION.toString(),null);
        }
        return (ApiResult) point.proceed();
    }

    @Around("teacherAspect()")
    public ApiResult aroundTeacher(ProceedingJoinPoint point)throws Throwable{
        String token= AspectUtils.getToken(point);
        if(AuthUtils.illegalUser(token,new int[]{User.TYPE_TEACHER})){
            return new ApiResult(false, Messages.NO_PERMISSION.toString(),null);
        }
        return (ApiResult) point.proceed();
    }

    @Around("studentAspect()")
    public ApiResult aroundStudent(ProceedingJoinPoint point)throws Throwable{
        String token= AspectUtils.getToken(point);
        if(AuthUtils.illegalUser(token,new int[]{User.TYPE_STUDENT})){
            return new ApiResult(false, Messages.NO_PERMISSION.toString(),null);
        }
        return (ApiResult) point.proceed();
    }
}
